package com.t.bricks.bussiness.service.dutyandperson.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.t.bricks.bussiness.db.dao.dutyandperson.DutyAndPersonDao;
import com.t.bricks.bussiness.db.entity.dutyandperson.DutyAndPerson;
import com.t.bricks.bussiness.service.dutyandperson.DutyAndPersonService;
import com.t.bricks.model.MsgEmity;

/**
 * 人员与职务关系表'DutyAndPerson表'基本业务操作类接口实现
 */
@Service
public class DutyAndPersonServiceImpl implements DutyAndPersonService<DutyAndPerson> {

	@Autowired
	private DutyAndPersonDao<DutyAndPerson> dutyAndPersonDao;

	/**
	 * 批量保存人员所属职务关联信息
	 * 注意:若职务信息为空则会清除所有该人员与职务的关联信息
	 * @param sPersonId 人员编号
	 * @param sDutyList 职务编号集合,以','分隔
	 * @return
	 */
	@Override
	public MsgEmity save(String sPersonId, String sDutyList) {
		if (null == sPersonId || "".equals(sPersonId.trim())) {
			return new MsgEmity(false, "人员编号参数不能为空！", 8001);
		}
		
		if (null == sDutyList || "".equals(sDutyList.trim())) {
			return dutyAndPersonDao.del(sPersonId.trim());
		}

		sPersonId = sPersonId.trim();

		List<DutyAndPerson> dutyList = new ArrayList<DutyAndPerson>();
		List<String> list = Arrays.asList(sDutyList.split(","));// 职务编号集合转换
		for(int i = 0; i < list.size(); i++) {
			String sDutyId = list.get(i).trim();
			if ("".equals(sDutyId)) {
				continue;
			}
			
			DutyAndPerson pd = new DutyAndPerson();
			pd.setsDutyId(sDutyId);
			pd.setsPersonId(sPersonId);
			dutyList.add(pd);
		}
	 
		return dutyAndPersonDao.save(sPersonId, dutyList);
	}
	
	/**
	 * 根据人员编号取人员与职务关联信息
	 * @param sPersonId
	 * @return
	 */
	@Override
	public MsgEmity findBysPersonId(String sPersonId) {
		if (null == sPersonId || "".equals(sPersonId.trim())) {
			return new MsgEmity(false, "人员编号参数不能为空！", 8001);
		}
		
		return dutyAndPersonDao.findBysPersonId(sPersonId.trim());
	}

}
